// ActionScript file
import components.downloadWindow;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.*;

import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.ComboBox;
import mx.controls.Label;
import mx.controls.Text;
import mx.events.CloseEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import vclass.Vhttp;

private function loadLang(whatLang:String):void{
	var loader:URLLoader = new URLLoader();
	var request:URLRequest = new URLRequest(domeniu+'languages/'+whatLang+'.xml');
	loader.load(request);
	function complete():void{
   		l = new XML(loader.data);
   		loader.removeEventListener(Event.COMPLETE,complete);
   		loader.removeEventListener(FaultEvent.FAULT,faultHandler);
   	}
   	function faultHandler(event:IOErrorEvent):void{
		Alert.show ("Error loading data from XML file!");
		loader.removeEventListener(ResultEvent.RESULT,complete);
		loader.removeEventListener(FaultEvent.FAULT,faultHandler);
	}
	loader.addEventListener(Event.COMPLETE,complete);
	loader.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
}
private function loadErrors(what:String):void{
	var loader:URLLoader = new URLLoader();
	var request:URLRequest = new URLRequest(domeniu+'languages/'+what+'.xml');
	loader.load(request);
	function complete():void{
   		messages = new XML(loader.data);
   		loader.removeEventListener(Event.COMPLETE,complete);
   		loader.removeEventListener(FaultEvent.FAULT,faultHandler);
   	}
   	function faultHandler(event:IOErrorEvent):void{
		Alert.show ("Error loading data from XML file!");
		loader.removeEventListener(ResultEvent.RESULT,complete);
		loader.removeEventListener(FaultEvent.FAULT,faultHandler);
	}
	loader.addEventListener(Event.COMPLETE,complete);
	loader.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
}
//set language
private function lSet(l:XML,index:Number):String{
	return l.row[index].@continut.toString();
}
private function messAlert(messid:Number, rpc:String):void{
	var mesaj:String = messages.row.(@id==messid).@continut+" RPC:"+rpc;
	function alertOkHandler(event:CloseEvent):void{
		this.currentState = 'login';
	}
	if(messid==6){
		//asta e cazul de looged out
		Alert.show(mesaj,"Error",4,this,alertOkHandler);
	}else{
		//alte erori
		Alert.show(mesaj,"Error");
	}
}
private function confirmAlert(messid:Number):void{
	var mesaj:String = messages.row.(@id==messid).@continut;
	Alert.show(mesaj);
}
public function confirmareOp(my:Vhttp,withConfirmare:Boolean=true):void{
 	var hehe:XML = new XML(my.getXML);
 	var flag:Number = hehe.attribute("flag");
	var messid:Number = hehe.attribute("messid");
	var myrpc:String = hehe.attribute("rpc");
	if(flag==0){
		messAlert(messid,myrpc);
	}else{
		if(withConfirmare){
			confirmAlert(messid);
		}
	}
}

private function checkSpecialCode(marca:String, model:String, companie:String):void{
	//some resets
	clImagLoaderNewImei.unloadAndStop();
	codBruteForce = false;
	codAlcatel = false;
	codIphone = false;
	//functia verifica daca este nevoie de vreun cod special pentru marca sau model
	if(marca == "BRUTE_FORCE")
	{
		codBruteForce = true;
		return;
	}
	if(marca == "Alcatel")
	{
		//check si model
		var modele:Array = new Array("C551","C552","C650","ELLE","OT-C701");
		for (var i:int = 0; i < modele.length; ++i)
		{
			if(model == modele[i]){
				//afisare elemente suplimentare
				clImagLoaderNewImei.load(domeniu+"imag/alcatel_unlock.jpg");
				codAlcatel = true;
				return;
			}
		}
	}
	
	if(marca == "I-PHONE" && companie == "Orange (Amena)-Spain")
	{
		codIphone = true;
		return;
	}
	return;
}
private function refreshModels(myPrice:Text,cbMarca:ComboBox):void{
 	modelList = phones.marca.(@nume==cbMarca.selectedLabel).model.@nume;
 	modelListCol = new XMLListCollection(modelList);
 	
 	companieList = phones.marca.(@nume==cbMarca.selectedLabel).model[0].companie.@nume;
 	companieListCol = new XMLListCollection(companieList);
 	
 	myPrice.text = phones.marca.(@nume==cbMarca.selectedLabel).model[0].companie[0].@cost+" euro";
 }
 private function refreshCompanie(myPrice:Text,cbModel:ComboBox):void{
 	companieList = phones.marca.model.(@nume==cbModel.selectedLabel).companie.@nume;
 	companieListCol = new XMLListCollection(companieList);
 	
 	myPrice.text = phones.marca.model.(@nume==cbModel.selectedLabel).companie[0].@cost+" euro";
 }
 private function refreshPrice(myPrice:Text,cbMarca:ComboBox,cbModel:ComboBox,cbCompanie:ComboBox):void{
 	myPrice.text = phones.marca.(@nume==cbMarca.selectedLabel).model.(@nume==cbModel.selectedLabel).companie.(@nume==cbCompanie.selectedLabel).@cost+" euro";
 }
 
 private function decideDownload(event:Event):void{
 		var flag:String = event.currentTarget.data.flag;
 		if(flag == "1"){
 			//send request for download
 			var file:String = event.currentTarget.data.mess;
 			var myDown:downloadWindow = new downloadWindow();
 			this.addChild(myDown);
 			myDown.x = canvas1.width/2-myDown.width/2;
 			myDown.y = canvas1.height/2-myDown.height;
 			myDown.fileName = file;
 			//download(file);
 		}else{
 			//eroare, afisare alert
 			var eroare:String = event.currentTarget.data.mess;
 			Alert.show(eroare,"Eroare");
 		}
 		event.currentTarget.removeEventListener(Event.COMPLETE,decideDownload);
 }
 private function formatDataIn(item:Date):String
{
	var monthLabels:Array = new Array("January","February","March",
      "April","May","June","July","August","September","October","November","December");

	if (item == null) {
        return "";
    }
    var luna:String = monthLabels[item.getMonth()];
    var zi:String = item.getDate().toString();
    var an:String = item.getFullYear().toString();
    return zi+" "+luna+" "+an;
}
 //calcul memorie
 /*
 [Bindable]
private var memorie:String;

private var checkMemoryIntervalID:uint = setInterval(checkMemoryUsage,1000);

private function checkMemoryUsage():void{
	memorie = ((System.totalMemory/1048576).toFixed(2)).toString()+" MB";
}
*/